﻿using Abp.Domain.Services;
using Aop.Api;
using shenghua.Basics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Payment.ThirdParties.Alipay
{
    public class AlipayConfig: DomainService
    {
        // 应用ID,您的APPID
        //public static string AppId = "2016091900551255";
        
        //// 商户私钥，您的原始格式RSA私钥
        //public static string AppPrivateKey = @"MIIEpAIBAAKCAQEAwzL5bx993Wbe1KEHK1RJSifS+oQtbE2SIgGUxWw8SDiKHUuJFktY9xq4yUogW8kpoJpkf7p8F13HZY+U6A36CqifGk9an9GX8wGuqNEtL84XwYI0qC+43aBvTXv4F2Ir7Q1/Hhz3NTu0qoY2L2KLdbDXI5mf8GGoI3DxkKAyYnHWVeaewpxlukbosT90MRA3rIs2OvM0enOjgH81Hb8AC3JMqP4pY77tL5K0KgrYiHwgpXanZ+1aImDDV4sTlcVT44K8XmzoGjUTW9BXEE++fKbxwJQPHFoCT1rAZdGi+MEQ4jCb6AeJ5I/ryG4rILWz+/T/pQye7yYlntw3o9cvKQIDAQABAoIBAQCogNJuKONAQtNM/gzwTHxP9XzdIUkM2xGxJWhF3J9VQOmJTHF0u2p7Nagaq1Cvq0GyEFV9JYCFs0Cz3XSPtbEJjnJ6BdKXcBkZf3RE6v107RX4x+uo3DlbqGksnD2YAfklU6dOWXbVj0rwgZc45Hq7CANQCgfVPJgmsBL35DJaY6S1NvsfxeujpcGJV7d6hKyxz4EfzivRksIzu++FCBasAMjpfrgufoCednJgnqwnJSoTvEsLUFhHLarwXz+2JHosBlazPbtJ8Uc1wyRrbvfYKu+23PtpEY9lKjtAiKasmIKFjeUr6pcGq4fKgnZzWa0sBzCeq37vamhydViN2qn5AoGBAOSwcsOTYFyedn4h5zTDdFDuWXZ4rYGi+wGQNod/2x9Zg6rwdGHx2hGtuhC7Q6yPtf+Mkkp3kkq/g9ZLs6nDM8POaPqsjBuqguOaLDP/tC/Qw18VLPSjVB8F8EFy2EyyIUiTrGHk+W6uomlUtu4C+8jSVOpMAdxaGprXdeRs/QIbAoGBANqCqAdIPobqfBv5FuJfwKqYBSevKWHvXGETCR9tJernNBZI6C6cSBGPHi9AeRWE8ePXlbt4CZfPOFKEBp5HleDX2zeZ8p2mF/VsgV2dyrh6TMozqrcl0jNeJsWsESIfo9mVQltTgKGGxQ229PJtMc2GXnnws/ZhfVtQ+JsvKcgLAoGAQe75d5ezDaEGSsFdlqHPlFHT60tJwvmJYt8ASbq8I2K8ink/JMoE9FnhLle/YC4i6+XMe7nTrMdgrFUxHH3SG98T90nuLz/O8u4NVoSfn7WRzoUda/73Dua4aTZCVIJ7BCDHb4YDwDyLSNEK2L51d2Xk45Q8aRp1VzVNoSknHQcCgYBh9s3qZeNSjXITAHvYBeHVGc7F2ewNskiSZPxkEIU9aWaazxj5PKu645U+zgL7ON1Rrue8Yra6TvcLV5G4CJQTd/MaXHS/ZSW8rGbbV9D5ICupQ7y328b/rKhpWxlbWSAWfqp/U1KKGGfkHDB5N6dqY2Gb/eRRA6g19nle6agrvQKBgQClD2y9wh3Uzv26rhJPsLw1V8s/TJ5y6wHMIeHT8ZrO8e8TmqNGRNTUDJvC0Sjm+ybKBYQxMDs3bAqjCzpPqGStuuGkf86qSvzYLHPIxzHZPAB5YnvIgnBsn234aNwzPUGtYlsx0WDoPDGB5JVcK9a8T8rZ+0hCsv7aBDCGtzh6sA==";

        //// 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
        //public static string AlipayPublicKey = @"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwPDQwWb1esWa201s/ScPR7VX9nsOTJ0e8f+qOER4J18SzBtn8/ZtYakuhHhXfmgpiHv0OKDzefAuQAXSnrsJMYMAv4HpUjRX7Syqa/Lc+yT6R8nAyaYTVF9XfcdoHoIggHMgMymEmeT8ruCNbiEb7ni4MBnCvoigk0n8TjpXQqFtAm5YoJlNsxtUFH5dBHY3+1l8gUY1dqFXDwdyMqm7gNnzvi6ewpCw/HmfgMrPHgyIGz3CCxltTs8/smv5ZVi7ns9lAkxnBhHa/pI+JAdWZwDo4QoxBtR4429U7xebYzdD/U/6zcPC3NfD+NqWV/FsGvuwWhJ3O1wVZ1Jctoc03wIDAQAB";

        private readonly SettingManager _settingManager;

        private bool _inited = false;

        public string AppId { get; private set; }
        public string AppPrivateKey { get; private set; }
        public string AlipayPublicKey { get; private set; }

        public string ReturnUrl { get; private set; }
        public string NotifyUrl { get; private set; }

        public AlipayConfig(
            SettingManager settingManager
            )
        {
            _settingManager = settingManager;
        }

        // 支付宝网关
        public static string GatewayUrl = "https://openapi.alipay.com/gateway.do";

        // 签名方式
        public static string SignType = "RSA2";

        // 编码格式
        public static string Charset = "UTF-8";

        public void InitConfigFromSetting()
        {
            if (_inited) return;

            var config = _settingManager.GetAlipayConfigInfo();
            AppId = config.AppId;
            AppPrivateKey = config.AppPrivateKey;
            AlipayPublicKey = config.AlipayPublicKey;
            ReturnUrl = config.ReturnUrl;
            NotifyUrl = config.NotifyUrl;
            _inited = true;
        }

        public IAopClient GetAopClientInstance()
        {
            InitConfigFromSetting();
            return new DefaultAopClient(GatewayUrl, AppId, AppPrivateKey, "json", "1.0", "RSA2", AlipayPublicKey);
        }
    }
}
